# -*- coding: utf-8 -*-
'''
Created on 25/01/2013

@author: pln
'''

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import sys
import qrc_resources
#import tabela as ships  
import re

try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class MainWindow(QMainWindow):

    NextId = 1 
    Instances = set()

    def __init__(self, filename = QString(), parent=None):

        super(MainWindow, self).__init__(parent)
       
        self.setAttribute(Qt.WA_DeleteOnClose)

        MainWindow.Instances.add(self)

        self.tabWidget = QTabWidget(self) 
        self.tabWidget.setGeometry(QRect(40,40, 1100, 850))
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))     
                 
        self.tabMarcacao = QWidget() #FirstTab
        self.tabMarcacao.setObjectName(_fromUtf8("tabMarcacao"))       
        
        self.nometexto = QLabel(self)
        self.nometexto.setGeometry(QRect(55,57,50,50))
        self.nometexto.setObjectName(_fromUtf8("Texto"))
        self.nometexto.setText(QApplication.translate("MainWindow", "Texto", None, QApplication.UnicodeUTF8))  #criancao das tabs
                
        self.editor = QTextEdit(self.tabMarcacao)                   
        self.editor.setGeometry(30,50,470,700)
    
        self.openFeatureButton = QPushButton(self.tabMarcacao)
        self.openFeatureButton.setGeometry(QRect(640, 600, 110, 37))
        self.openFeatureButton.setText(QApplication.translate("None","Abrir", None, QApplication.UnicodeUTF8))       
          
        self.connect(self.openFeatureButton , SIGNAL("clicked()"), self.fileOpen)
    
        self.tabWidget.addTab(self.tabMarcacao, _fromUtf8(""))

#----------------------------------------------------------------------------------------------------------------
        
        self.tabPreProcessamento = QWidget() #SecondTab                              
        self.tabPreProcessamento.setObjectName(_fromUtf8("tabPreProcessamento"))
        
        self.nometexto = QLabel(self)
        self.nometexto.setGeometry(QRect(55,57,50,50))
        self.nometexto.setObjectName(_fromUtf8("Texto"))
        self.nometexto.setText(QApplication.translate("MainWindow", "Texto", None, QApplication.UnicodeUTF8))  #criancao das tabs        
        
        self.editor = QTextEdit(self.tabPreProcessamento)                   
        self.editor.setGeometry(30,50,470,700) 

        self.openFeatureButton = QPushButton(self.tabPreProcessamento)
        self.openFeatureButton.setGeometry(QRect(640, 600, 110, 37))
        self.openFeatureButton.setText(QApplication.translate("None","Abrir", None, QApplication.UnicodeUTF8))       
                      
        self.tabWidget.addTab(self.tabPreProcessamento, _fromUtf8(""))
        
#----------------------------------------------------------------------------------------------------------------   

        self.tabOntologia = QWidget() #Third tab
        self.tabOntologia.setObjectName(_fromUtf8("tabOntologia"))
        
        self.nometexto = QLabel(self)
        self.nometexto.setGeometry(QRect(55,57,50,50))
        self.nometexto.setObjectName(_fromUtf8("Texto"))
        self.nometexto.setText(QApplication.translate("MainWindow", "Texto", None, QApplication.UnicodeUTF8))  #criancao das tabs        
        
        self.editor = QTextEdit(self.tabOntologia)                   
        self.editor.setGeometry(30,50,470,700)        
        
        self.openFeatureButton = QPushButton(self.tabOntologia)
        self.openFeatureButton.setGeometry(QRect(640, 600, 110, 37))
        self.openFeatureButton.setText(QApplication.translate("None","Abrir", None, QApplication.UnicodeUTF8))       
        
        self.tabWidget.addTab(self.tabOntologia, _fromUtf8(""))

#----------------------------------------------------------------------------------------------------------------
   
        self.tabLexico = QWidget() #Fourth tab
        self.tabLexico.setObjectName(_fromUtf8("tabLexico"))
        
        self.nometexto = QLabel(self)
        self.nometexto.setGeometry(QRect(55,57,50,50))
        self.nometexto.setObjectName(_fromUtf8("Texto"))
        self.nometexto.setText(QApplication.translate("MainWindow", "Texto", None, QApplication.UnicodeUTF8))  #criancao das tabs        
        
        self.editor = QTextEdit(self.tabLexico)                   
        self.editor.setGeometry(30,50,470,700)        

        self.openFeatureButton = QPushButton(self.tabLexico)
        self.openFeatureButton.setGeometry(QRect(640, 600, 110, 37))
        self.openFeatureButton.setText(QApplication.translate("None","Abrir", None, QApplication.UnicodeUTF8))               
        
        self.tabWidget.addTab(self.tabLexico, _fromUtf8(""))

#----------------------------------------------------------------------------------------------------------------

        self.tabResultado = QWidget() #Fifth tab
        self.tabResultado.setObjectName(_fromUtf8("tabResultado"))
        
        self.nometexto = QLabel(self)
        self.nometexto.setGeometry(QRect(55,57,50,50))
        self.nometexto.setObjectName(_fromUtf8("Texto"))
        self.nometexto.setText(QApplication.translate("MainWindow", "Texto", None, QApplication.UnicodeUTF8))  #criancao das tabs
        
        self.editor = QTextEdit(self.tabResultado)                   
        self.editor.setGeometry(30,50,470,700)       

        self.openFeatureButton = QPushButton(self.tabResultado)
        self.openFeatureButton.setGeometry(QRect(640, 600, 110, 37))
        self.openFeatureButton.setText(QApplication.translate("None","Abrir", None, QApplication.UnicodeUTF8))               
                 
        self.tabWidget.addTab(self.tabResultado, _fromUtf8(""))

#----------------------------------------------------------------------------------------------------------------
   
        self.resize(1500, 1200)
        self.scroll(400,400)

        self.setWindowTitle(QApplication.translate("Form", filename, None, QApplication.UnicodeUTF8))

        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabMarcacao), QApplication.translate("Form", "Marcação Manual", None, QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabPreProcessamento), QApplication.translate("Form", "Pré Processamento", None, QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabOntologia), QApplication.translate("Form", "Ontologia", None, QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabLexico), QApplication.translate("Form", "Léxico de Sentimento", None, QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabResultado), QApplication.translate("Form", "Resultados", None, QApplication.UnicodeUTF8))
        
        self.filename = filename
        
        if self.filename.isEmpty():
            self.filename = QString("Unnamed-%d.txt" % MainWindow.NextId)
            MainWindow.NextId += 1
            self.editor.document().setModified(False)
            self.setWindowTitle("MAIN WINDOW  - %s" % self.filename)
        else:
            self.loadFile()

    def fileOpen(self):
        filename = QFileDialog.getOpenFileName(self,
                            "PROGRAMA TEXTOS -- Open File")     #abertura da file
        if not filename.isEmpty():
            if not self.editor.document().isModified() and \
                self.filename.startsWith("Unnamed"):
                self.filename = filename
                self.loadFile()                                #utiliza o load onde está a abertura do owl
            else:
                MainWindow(filename).show()

app = QApplication(sys.argv)
MainWindow().show()
app.setWindowIcon(QIcon(":/icon.png"))  
app.exec_()
